#include "process.h"

#define NUM 101
#define SPEED_POINT 10

#define STYLE '='
#define POINT '.'
#define SPACE ' '

const int len = 3;

// 根据下载量，来进行动态刷新的进度条
void FlushProcess(double current, double total)
{
    // 1.更新当前的进百分比
    double rate/*比率*/ = current / total * 100;
    //printf("test:%.1lf%%[%.1lf:%.1lf]\r", rate, current, total);
    //fflush(stdout);
    // 2.打印进度条
    char bar[NUM];
    memset(bar, '\0', sizeof(bar));
    for(int i = 0; i < (int)rate; ++i)
    {
        bar[i] = STYLE;
    }

    static int speed_point = 0;
    static int num = 0;
    if(speed_point == SPEED_POINT) {++num; speed_point = 0;}
    ++speed_point;
    char points[len+1];
    for(int i = 0; i < len; ++i)
    {
        if (i <= num % len) points[i] = POINT;
        else points[i] = SPACE;
    }
    // 下载完成的时候打印...全部的点
    if(current == total) 
    {
        memset(points, '.', sizeof(points));
        num = 0; // 一次下载完毕 把num清零 为下一次打印准备
    }
    points[len] = '\0';

    printf("[%-100s][%.1lf%%]%s\r", bar, rate, points);
    fflush(stdout);
    
}








//void Process()
//{
//    const char* lable = "|/-\\";
//    size_t len = strlen(lable);
//    char bar[NUM];
//    memset(bar, '\0', sizeof(bar));
//    
//    int cnt = 0;
//    while(cnt <= 100)
//    {
//        printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt%len]);
//        fflush(stdout);
//        
//        bar[cnt++] = STYLE;
//
//        if (cnt != 100)
//            bar[cnt] = '>';
//
//        //sleep(1); 单位秒
//        usleep(100000); //单位微秒 
//    }
//
//    printf("\r\n");
//}
